﻿#ifndef TABLE_H
#define TABLE_H

#if defined(_MSC_VER) && (_MSC_VER >= 1600)
# pragma execution_character_set("utf-8")
#endif

#include <QObject>
#include <QTime>
#include <QDateTime>
#include <QVector>
#include <QMutex>
#include <QSerialPort>
#include "myhelper.h"

class Error
{
public:
    Error(QString strNE) : strError(strNE) {}
    QString strError;
};

/**系统参数*/
class System
{

public:
    QString filePath;   //录音文件路径
    QString localHost;  //本机地址
    int     httpPort;   //http服务端口
    int     fileExpireDay;  //文件过期天数
    int     dataExpireDay;  //数据过期天数
};

class Database
{
public:
    QString ip;
    int     port;
    QString userName;
    QString password;
    QString databaseName;
};

class Cos//参数
{
public:
    float cosjx_1;
    float cosjx_2;
    float coszy_1;
    float coszy_2;
};

/**串口参数*/
class Serial
{

public:

    QString     serialName;         //串口号
    int         baudRate;           //波特率
    int         dataBit;            //数据位
    int         checkoutBit;        //校验位
    int         stopBit;            //停止位

    QSerialPort::DataBits GetDataBit(){
        QSerialPort::DataBits bit;
        if(this->dataBit == 5){
            bit = QSerialPort::Data5;
        }else if(this->dataBit == 6){
            bit = QSerialPort::Data6;
        }else if(this->dataBit == 7){
            bit = QSerialPort::Data7;
        }else if(this->dataBit == 8){
            bit = QSerialPort::Data8;
        }else{
            //bit = QSerialPort::UnknownDataBits;
        }
        return bit;
    }

    QSerialPort::Parity GetParity(){
        QSerialPort::Parity parity;
        if(this->checkoutBit == 0){
            parity = QSerialPort::NoParity;
        }else if(this->checkoutBit == 1){
            parity = QSerialPort::EvenParity;
        }else if(this->checkoutBit == 2){
            parity = QSerialPort::OddParity;
        }else if(this->checkoutBit == 3){
            parity = QSerialPort::SpaceParity;
        }else if(this->checkoutBit == 4){
            parity = QSerialPort::MarkParity;
        }else{
            //parity = QSerialPort::UnknownParity;
        }
        return parity;
    }

    QSerialPort::StopBits GetStopBit(){
        QSerialPort::StopBits bit;
        if(this->stopBit == 0){
            bit = QSerialPort::OneStop;
        }else if(this->stopBit == 1){
            bit = QSerialPort::OneAndHalfStop;
        }else if(this->stopBit == 2){
            bit = QSerialPort::TwoStop;
        }else{
            //bit = QSerialPort::UnknownStopBits;
        }
        return bit;
    }
};

class EnumType
{

public:

    static QString GetSwitchName(int type){
        QString name = EnumType::GetSwitchGroup().at(type);
        return name;
    }
    static QStringList GetSwitchGroup(){
        QStringList types;
        types << "1.固定监测" << "2.射频激励" << "3.高末屏压" << "4.系统内部" << "5.系统外部" << "6.发射机输入" << "7.发射机输出" << "8.发射机输出";
        return types;
    }
};

//----------------------------------------------------

/**用户*/
class User
{

public:
    QString     code;
    QString     password;
    QString     name;
    int         role;
    bool        builtin;

    bool        login = false;

    QString GetRole(){
        QString text = "";
        if(role == 0){
            text = "管理员";
        }else if(role == 1){
            text = "机房主任";
        }else if(role == 2){
            text = "操作员";
        }
        return text;
    }
};

/**日志*/
class SysLog
{

public:
    int         id;
    int         type;
    QString     operater;
    QString     describe;
    QDateTime   createTime;

    QString GetTypeText(){
        QString text = "";
        if(type == 0){
            text = "系统日志";
        }else if(type == 1){
            text = "操作日志";
        }else if(type == 2){
            text = "收测日志";
        }
        return text;
    }
};


//--------------------------------------------

class IPData
{
public:
    QString Kw;
    QString IP;
    int     Port=0000;
};

struct TcpRead
{
     quint8  HEAD=0xFF;    //帧头
     quint8  AGREE;  //协议
     quint8  Reserved=0x00;   //预留/状态
     quint8  Computer;    //主机
     quint8  ComputerB;   //备机
     quint8  Tower;     //主塔
     quint8  TowerB;     //备塔
     quint8  CRC=0xED;     //校验

};
/**监测设备*/
class Equipment
{

public:
    QString     devCode;            //设备代号
    quint8      devid;              //设备号
    QString     devname;            //设备名称
//    int         type = 0;           //发射机类型 0-10KW 1-50KW

    QString     serialName;         //串口号
    int         baudRate;           //波特率
    int         dataBit;            //数据位
    int         checkoutBit;        //校验位
    int         stopBit;            //停止位

    float       cosrs = 20;         //入射系数
    float       cosfs = 30;         //反射系数
    float       cosgp = 50;         //高频系数
    float       cos_140 = 50;       //-140V系数
    float       cos_18 = 15;        //-18V系数
    float       cos_12 = 35;        //12V系数
    float       cos_z18 = 100;      //18V系数
    float       cos_24 = 40;        //24V系数
    float       cosyp= 50;          //音频信号系数
    float       coszf  = 20;        //中放电流系数
    float       costz = 25;         //调制电流系数
    float       costd = 16;         //推动电流系数

    //-----------------------------
    bool        isOnline = false;

signals:
    void Update();
};

/**状态量*/
struct State
{
    QString     devname;
    QDateTime   datetime;
    QString     data;
    QList<int>  dataList;

    QList<int> getDataList(){
        if(this->dataList.size() == 16){
            return this->dataList;
        }

        QList<int> dataList;
        QStringList strs = this->data.split(",");
        for(QString s : strs){
            dataList.append(s.toInt());
        }
        if(dataList.size() < 16){
            for(int i=dataList.size(); i<16; i++){
                dataList.append(0);
            }
        }
        return dataList;
    }
};

/**模拟量*/
class Simulation
{
public:
    quint8      devid=0x00;
    QString     devname;
    float   launch = 0;          //入射
    float   reflect = 0;         //反射
    float   highFreq=0;          //高频
    float   FOFZ=0;              //-140
    float   FOE=0;               //-18
    float   OT=0;                //12
    float   OE=0;                //18
    float   TF=0;                //24
    float   audio=0;             //音频信号
    float   putting=0;           //中放电流
    float   modulate=0;          //调制电流
    float   drive=0;             //推动电流
    float   fre=0;               //频率
    QDateTime   datetime;
};

/**指标数据*/
class Quality
{

public:
    //int         id;
    QString     code;            //设备名称
    QDateTime   startTime;      //开始时间
    QDateTime   endTime;        //结束时间
    QString     data;           //扫描结果

   // Simulation   sim;
};

/**指标报警*/
class QualityAlarm
{

public:
    int         id;
    QString     code;           //设备代号
    int         type;           //类型 0-温度 1-湿度
    QDateTime   startDate;      //开始时间
    QDateTime   endDate;        //结束时间
    float       value = 0;      //报警值
    int         relieve = 0;    //0-报警  1-解除
    QString     reason;

    //--------------------------
    Equipment   equ;

    Equipment   *param;
    int         save = 0;           //0-不需要保存 1-保存报警 2-已保存报警 3-保存解除报警

    void CheckAlarm(float temp, float hum);//判断温湿度是否到达门限 是否需要报警
    void CheckTemp(float temp);
    void CheckHum(float hum);

    QString GetTypeText(){
        QString text = QString::number(type);
        if(type == 0){
            text = "温度";
        }else if(type == 1){
            text = "湿度";
        }
        return text;
    }

    QString GetRelieveText(){
        QString text = "";
        if(relieve == 0){
            text = "未解除";
        }else if(relieve == 1){
            text = "已解除";
        }
        return text;
    }
};

#endif // TABLE_H
